package com.dingli.DAO;

import com.dingli.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;

public class TicketDAO {
    private QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSource());

    public boolean purchaseTicket(String personID, String tktID){
        String sql = "insert into ticketPurchased(personID,tktID)values (?,?);";
        try {
            BigInteger res = runner.insert(sql,new ScalarHandler<BigInteger>(), personID, tktID);
            return res.intValue() > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int createTicket(String tLv, int lineID, String tktNum, String tktPrice){
        String sql = "insert into Ticket(tlevel,lineID,restNumber,price)values(?,?,?,?)";
        try {
            int lv = Integer.parseInt(tLv);
            int num = Integer.parseInt(tktNum);
            double price = Double.parseDouble(tktPrice);
            BigInteger res = runner.insert(sql,new ScalarHandler<BigInteger>(), lv, lineID, num, price);
            return res.intValue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

